# -*- coding: UTF-8 -*-
import os
import sys
import argparse
import yaml
import importlib

def load_config():
    """加载配置文件"""
    with open('conf/conf.yaml', 'r', encoding='utf-8') as file:
        config = yaml.safe_load(file)
    return config

def validate():
    parser = argparse.ArgumentParser()
    parser.add_argument('action', type=str, choices=['sync', 'stat', 'push'], help='执行类型 (sync, stat, push)')
    parser.add_argument('date', type=str, help='执行日期 (格式：YYYYMMDD)')

    args = parser.parse_args()
    return args.action, args.date

def main():
    action, run_date = validate()
    config = load_config()

    # 动态导入模块并调用 action 函数
    try:
        module_path = os.path.join(os.getcwd(), f"{action}.py")
        spec = importlib.util.spec_from_file_location(action, module_path)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        if hasattr(module, 'action'):
            module.action(config, run_date)
        else:
            print(f"模块 {action} 中没有定义 action 函数。")
    except ModuleNotFoundError:
        print(f"找不到模块 {action}.py.")


if __name__ == "__main__":
    main()